package com.java.jvalidator.framework.config;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.java.jvalidator.framework.TypeUtil;
import com.java.jvalidator.framework.ValidationException;
import com.java.jvalidator.framework.ValidationExceptionMsg;
import com.java.jvalidator.framework.ValidationResult;

class ClassConfig {
	// <字段名,PropertyConfig对象>
	private Map<String, PropertyConfig> fieldsConfig;
	// 关联的class名
	private String targetClassName;

	ClassConfig(String className) {
		this.targetClassName = className;
		this.fieldsConfig = new HashMap<String, PropertyConfig>();
	}

	ValidationResult validate(Object target) throws ValidationException {
		return validate(target, null);
	}

	ValidationResult validate(Object target, String pattern) throws ValidationException {
		if (this.fieldsConfig.size() == 0) {
			return new ValidationResult();
		}
		if (!target.getClass().getName().equals(this.targetClassName)) {
			throw new ValidationException(ValidationExceptionMsg.CLASS_NAME_NOT_EQUAL_ERROR);
		}
		Collection<PropertyConfig> configs = this.fieldsConfig.values();
		Iterator<PropertyConfig> iter = configs.iterator();
		ValidationResult result = new ValidationResult();
		// 一个class包含多个Property
		// 一个Property对应一个PropertyConfig
		// 一个Property对应多条check规则(RuleConfig)
		while (iter.hasNext()) {
			PropertyConfig config = (PropertyConfig) iter.next();
			// 如果pattern非空，那么只check PropertyName匹配pattern的字段，过滤掉其他字段的check
			if ((!TypeUtil.isStringNullOrEmpty(pattern)) && (!config.getPropertyName().matches(pattern))) {
				continue;
			}
			result.addResult(config.validate(target));
		}

		return result;
	}

	void addFieldConfig(PropertyConfig fieldConfig) throws ValidationException {
		if (fieldConfig == null) {
			return;
		}
		if (!this.targetClassName.equals(fieldConfig.getTargetClassName())) {
			throw new ValidationException(ValidationExceptionMsg.CLASS_NAME_NOT_EQUAL_ERROR);
		}
		if (this.fieldsConfig.get(fieldConfig.getPropertyName()) != null) {
			return;
		}
		this.fieldsConfig.put(fieldConfig.getPropertyName(), fieldConfig);
	}

	String getTargetClassName() {
		return this.targetClassName;
	}

	void configCustomValidator(String fieldName, String customValidatorClassName, Parameters params) throws ValidationException {
		PropertyConfig config = (PropertyConfig) this.fieldsConfig.get(fieldName);
		if (config == null) {
			throw new ValidationException(ValidationExceptionMsg.FIELD_CONFIG_NOT_FOUND_ERROR + fieldName);
		}
		config.configCustomValidator(customValidatorClassName, params);
	}

	void setPlaceHolderValues(String propertyName, String errorMsgName, String[] strings) {
		if ((propertyName == null) || (errorMsgName == null)) {
			return;
		}
		PropertyConfig fieldConfig = (PropertyConfig) this.fieldsConfig.get(propertyName);
		if (fieldConfig != null) {
			fieldConfig.setPlaceHolderValues(errorMsgName, strings);
		}
	}
}
